package com.sakila.dao;




import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sakila.dbo.AdressDBO;
import com.sakila.util.DBConnection;

public class AdressDAO implements IAddressDAO {

	
	
	




	  private Logger logger = Logger.getLogger(this.getClass());
	//SimpleDateFormat formatter = new SimpleDateFormat(
		//	"yyyy.MM.dd G 'at' HH:mm:ss z");

	/* (non-Javadoc)
	 * @see com.sakila.dao.IAddressDAO#findById(int)
	 */
	@Override
	public AdressDBO findById(int addressId) {
		AdressDBO dbo = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append(" select address_id as addressId, address as address, district as district, city_id as cityId, postal_code as postalCode, phone as phone");
			sb.append("from address ");
			sb.append("where  address_id = " + addressId);
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here in Address DAO: "+ sb.toString());
			rs = statement.executeQuery();
			if (rs.next()) {
				dbo = new AdressDBO();
				dbo.setAddress_id(rs.getInt("addressId"));
				dbo.setAddress(rs.getString("address"));
				dbo.setDistrict(rs.getString("district"));
				dbo.setCity_id(rs.getInt("cityId"));
				dbo.setPostal_code(rs.getString("postalCode"));
				dbo.setPhone(rs.getString("phone"));
				
				
			}

		} catch (SQLException e) {
			logger.error("Exception in Address DAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
		return dbo;
	}

	/* (non-Javadoc)
	 * @see com.sakila.dao.IAddressDAO#findAll()
	 */
	@Override
	public List<AdressDBO> findAll() {
		List<AdressDBO> returnList = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		AdressDBO dbo = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append(" select address_id as addressId, address as address, district as district, city_id as cityId, postal_code as postalCode, phone as phone");
			sb.append("from address ");
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here Address DAO infindAll: "+ sb.toString());
			rs = statement.executeQuery();
			returnList = new ArrayList<AdressDBO>();
			while (rs.next()) {
				dbo = new AdressDBO();
				dbo.setAddress_id(rs.getInt("addressId"));
				dbo.setAddress(rs.getString("address"));
				dbo.setDistrict(rs.getString("district"));
				dbo.setCity_id(rs.getInt("cityId"));
				dbo.setPostal_code(rs.getString("postalCode"));
				dbo.setPhone(rs.getString("phone"));
				returnList.add(dbo);
				
			}

		} catch (SQLException e) {
			logger.error("Exception in Address DAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
logger.debug("Display Me everyone from List:" + returnList);
		return returnList;
	}
	
}

